<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>三种方式盒子水平居中</title>
    <style>
        .common {
            width: 200px;
            height: 200px;
            background-color: burlywood;
            border: 1px solid black;
            
        }
        .common-son {
            width: 100px;
            height: 100px;
            background-color: red;
            font: 20px/100px "微软雅黑";
            text-align: center;
        }
        /* 子元素左右边距自动，子元素宽度小于父元素 */
        .father1 .son1 {
            margin: 0 auto;
        }
        .father2,.father3 {
            position: relative;
        }
        /* 子绝父相 */
        .father2 .son2 {
            position: absolute;
            left: 50%;
            margin-left: -50px;
        }
        /* 子绝父相,水平移动*/
        .father3 .son3 {
            position: absolute;
            left: 50%;
            transform: translateX(-50%);
        }
    </style>
</head>
<body>
    <div class="father1 common">
        <div class="son1 common-son">1</div>
    </div>
    <div class="father2 common">
        <div class="son2 common-son">2</div>
    </div>
    <div class="father3 common">
        <div class="son3 common-son">3</div>
    </div>
</body>
</html>